/**
 * @file   AbstractFileCheck.java
 * @date   2012-11-5下午4:32:48
 * @author User
 */
package com.gzceb.zhgjj.filecheck;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gzceb.zhgjj.model.Constants;
import com.gzceb.zhgjj.service.LoanAprvService;
import com.gzceb.zhgjj.util.PropertiesUtils;
import com.gzceb.zhgjj.util.SpringContextUtil;

/**
 * <p>
 * TTF001接收文件校验类。
 * </p>
 * 
 * @date 2012-11-5下午4:32:48
 * @author liqing
 * @description TTF001接收到的文件进行格式、内容上到校验
 */
public abstract class AbstractFileCheck implements FileCheck {

	private final Log logger = LogFactory.getLog(getClass());
	protected boolean isNamelegal = false; // 文件名是否合法
	protected String genRecdFlag; // 总记录标志位
	protected String respFileName; // 响应文件名
	protected String recvFileName; // 接收文件名
	protected List<String> fileCont; // 接收到文件后按行内容生成的List

	public AbstractFileCheck() {

	}

	

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#isNamelegal()
	 */
	public boolean isNamelegal() {
		return this.isNamelegal;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#setRespFileName()
	 */
	public void setRespFileName() {
		// TODO Auto-generated method stub
		if (this.isNamelegal == true) {
			logger.debug("the file name is legal.");
			this.respFileName = this.recvFileName + "1";
		} else {
			logger.debug("the file name is illegal");
			this.respFileName = this.recvFileName + "0";
		}

	}
	
	public String getRespFileName(){
		return this.respFileName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#getRecvFileName(java.io.File)
	 */
	public String getRecvFileName(File file) {
		return file.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#getRecvFileCont(java.io.File)
	 */

	public List<String> getRecvFileCont(File file) {
		
		List<String> strarr = new ArrayList<String>();
		BufferedReader reader = null;
		
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempstr = null;
			logger.debug("the file content: \n");
			while ((tempstr = reader.readLine()) != null) {
				strarr.add(tempstr);
				logger.debug(tempstr + "\n");
			}
			
		} catch (IOException e) {
			logger.error("error when read the file " + file.getAbsolutePath()
					+ ":\n" + e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception ec) {

				}
			}
		}

		return strarr;
	}

	public boolean isFormatedName(String filename) {
		Pattern p = Pattern.compile("L[ABCD]\\d{18}"); // TTF001格式文件名的正则格式
		Matcher m = p.matcher(filename);
		return m.matches();
	}

	public boolean isDateFormated(String filename) {
		String datestr = filename.substring(8, 16);
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		Date dt;
		try {
			dt = fmt.parse(datestr);
		} catch (Exception ec) {
			return false;
		}
		String tst = fmt.format(dt);
		return datestr.equals(tst);

	}

	public boolean isBankCode(String filename) {
		String bankCode = filename.substring(4, 8);
		//TODO 需完善从配置文件中读取银行代码
		return bankCode.equals(Constants.BANKID); // 是否符合接收银行代码
	}
	
	/**
	 * 创建响应文件
	 * @param null
	 * @return 响应文件绝对路径 String
	 */
	public String crtRespFile(){
		String respFileName = getRespFileName();
		String respFile = PropertiesUtils.getValue("rspPath") + respFileName;
		File file = null;
		try{
			file = new File(respFile);
			if(file.exists()){
				file.delete();	
			}
			file.createNewFile();
		}catch(NullPointerException ne){
			logger.error("errors happened when create response TTF001 file. \n" 
						+ ne.getMessage());
			respFileName = "";
		}catch(IOException ie){
			logger.error("IO errors happened when create response TTF001 file. \n" 
					+ ie.getMessage());
			respFileName = "";
		}
		//先设置总记录标志
		setGenRecdFlag();
		//根据总记录标志设置每行分记录标志
		setEachRecdFlag();
		//生成文件
		BufferedWriter writer = null;
		logger.info("begin to create response file...");
		try{
			writer = new BufferedWriter(new FileWriter(file));
			for(String tmpstr:fileCont){
				writer.write(tmpstr);
				writer.newLine();
			}
		}catch(IOException ie){
			logger.error("error happend when write the response file.\n" + ie.getMessage());
			respFileName = "";
		}finally{
			if (writer != null) {
				try {
					writer.close();
				} catch (Exception ec) {
					respFileName = "";
				}
			}
		}
		
		return respFileName;
	}

}
